package com.qfedu.spring.mybatis.service.impl;

import com.qfedu.spring.mybatis.dao.UserMapper;
import com.qfedu.spring.mybatis.entity.User;
import com.qfedu.spring.mybatis.service.UserService;

import java.util.List;
import java.util.Objects;

/**
 * @author zhang
 * @Title: commonValidateAdd
 * @date : 2022/4/27 16:38
 */
public class UserServiceImpl implements UserService {

    private UserMapper userMapper;

    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User login(String name, String password) {
        List<User> users = userMapper.selectByName(name);
        for (User user : users) {
            if (Objects.nonNull(user)){
                if (Objects.equals(user.getPassword(),password)){
                    //用户名和密码正确
                    return user;
                }
            }
        }

        return null;
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public int addUser(User user) {
//        int i = 1/0;
        return userMapper.add(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.update(user);
    }

    @Override
    public int deleteUser(Integer id) {
        return userMapper.delete(id);
    }
}
